package redis

import (
	"go_batch_create_orders/package/configIni"
	"strconv"
)

type Options func(*Redis)

type RedisSystem struct {
	Host               string `json:host`
	Password           string `json:"password"`
	Port               string `json:"port"`
	Database           int    `json:"database"`
	Switch             bool   `json:"switch"`
	NetWork            string `json:"network"`
	MaxIdle            int    `json:"maxIdle"`
	MaxActive          int    `json:"maxActive"`
	IdleTimeOut        int    `json:"idleTimeOut"`
	DialReadTimeout    int    `json:"dialReadTimeout"`
	DialWriteTimeout   int    `json:"dialWriteTimeout"`
	DialConnectTimeout int    `json:"dialConnectTimeout"`
}

type Redis struct {
	RedisSystem
}

func New() *Redis {
	//初始化 mysql链接信息
	system := RedisSystem{
		Host:               "127.0.0.1",
		Password:           "",
		Port:               "6379",
		Database:           0,
		Switch:             false,
		MaxIdle:            256,
		MaxActive:          0,
		IdleTimeOut:        120,
		DialReadTimeout:    1000,
		DialWriteTimeout:   1000,
		DialConnectTimeout: 1000,
		NetWork:            "tcp",
	}
	m := &Redis{
		RedisSystem: system,
	}

	options := m.SetOptions("redis")
	data := m.SetData(options)

	//查询当前的是否有状态配置信息
	config := configIni.Config{Module: "redis", Filed: "runmode"}
	fieldData := config.Config()
	if fieldData != "" {
		setOptions := data.SetOptions(fieldData)
		data = data.SetData(setOptions)
	}

	return data
}

func (m *Redis) SetOptions(module string) []Options {
	config := configIni.Config{Module: module}
	list := config.ConfigList()
	value := ""
	configs := configIni.Config{}
	options := make([]Options, 0, len(list))
	for _, v := range list {
		switch v {
		case "password":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			options = append(options, m.SetPassword(value))
			break
		case "port":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			options = append(options, m.SetPort(value))
			break
		case "database":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			parseInt, err := strconv.Atoi(value)
			if err != nil {
				break
			}
			options = append(options, m.SetDatabase(parseInt))
			break
		case "host":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			options = append(options, m.SetHost(value))
			break
		case "switch":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			parseBool, err := strconv.ParseBool(value)
			if err != nil {
				break
			}
			options = append(options, m.SetSwitch(parseBool))
			break
		case "maxIdle":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			parseInt, err := strconv.Atoi(value)
			if err != nil {
				break
			}
			options = append(options, m.SetMaxIdle(parseInt))
			break
		case "maxActive":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			parseInt, err := strconv.Atoi(value)
			if err != nil {
				break
			}
			options = append(options, m.SetMaxActive(parseInt))
			break
		case "idleTimeout":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			parseInt, err := strconv.Atoi(value)
			if err != nil {
				break
			}
			options = append(options, m.SetIdleTimeOut(parseInt))
			break
		case "dialReadTimeout":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			parseInt, err := strconv.Atoi(value)
			if err != nil {
				break
			}
			options = append(options, m.SetDialReadTimeout(parseInt))
			break
		case "dialWriteTimeout":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			parseInt, err := strconv.Atoi(value)
			if err != nil {
				break
			}
			options = append(options, m.SetDialWriteTimeout(parseInt))
			break
		case "dialConnectTimeout":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			parseInt, err := strconv.Atoi(value)
			if err != nil {
				break
			}
			options = append(options, m.SetDialConnectTimeout(parseInt))
			break
		case "network":
			configs = configIni.Config{Module: module, Filed: v}
			value = configs.Config()
			options = append(options, m.SetNetWork(value))
			break
		}

	}
	return options
}

func (m *Redis) SetData(option []Options) *Redis {
	for _, v := range option {
		v(m)
	}
	return m
}

func (redis *Redis) SetPassword(password string) Options {
	return func(redis *Redis) {
		redis.Password = password
	}
}

func (redis *Redis) SetHost(address string) Options {
	return func(redis *Redis) {
		redis.Host = address
	}
}

func (redis *Redis) SetPort(port string) Options {
	return func(redis *Redis) {
		redis.Port = port
	}
}

func (redis *Redis) SetDatabase(database int) Options {
	return func(redis *Redis) {
		redis.Database = database
	}
}

func (redis *Redis) SetSwitch(s bool) Options {
	return func(redis *Redis) {
		redis.Switch = s
	}
}

func (m *Redis) SetDialConnectTimeout(parseInt int) Options {
	return func(redis *Redis) {
		redis.DialConnectTimeout = parseInt
	}
}

func (m *Redis) SetDialWriteTimeout(parseInt int) Options {
	return func(redis *Redis) {
		redis.DialWriteTimeout = parseInt
	}
}

func (m *Redis) SetDialReadTimeout(parseInt int) Options {
	return func(redis *Redis) {
		redis.DialReadTimeout = parseInt
	}
}

func (m *Redis) SetIdleTimeOut(parseInt int) Options {
	return func(redis *Redis) {
		redis.IdleTimeOut = parseInt
	}
}

func (m *Redis) SetMaxActive(parseInt int) Options {
	return func(redis *Redis) {
		redis.MaxActive = parseInt
	}
}

func (m *Redis) SetMaxIdle(parseInt int) Options {
	return func(redis *Redis) {
		redis.MaxIdle = parseInt
	}
}

func (m *Redis) SetNetWork(value string) Options {
	return func(redis *Redis) {
		redis.NetWork = value
	}
}
